Skip to content

fix: WebSocket reconnection stability and manual pairing flow#2

Open
comlibmb wants to merge 3 commits into
Forkoff-app:mainfrom
comlibmb:main
Open

fix: WebSocket reconnection stability and manual pairing flow#2
comlibmb wants to merge 3 commits into
Forkoff-app:mainfrom
comlibmb:main

Conversation

@comlibmb
Copy link
Copy Markdown

@comlibmb comlibmb commented May 4, 2026

Summary

Fixes for WebSocket reconnection after tunnel restart and manual pairing code flow.

Changes

  1. WebSocket Service (services/websocket.service.ts)

    • pollTunnelUrl polls Supabase every 10s, detects tunnel URL change and auto-reconnects
    • Limit Socket.IO auto-reconnect attempts to 3 (was Infinity) to prevent conflicts
  2. App Layout (app/_layout.tsx)

    • AppState foreground handler always checks tunnel URL vs current relay URL
    • Reconnects if URL changed or connection lost
  3. Add Device (app/(onboarding)/add-device.tsx)

    • "Enter Code" mode now redirects to device/pair.tsx for reliable pairing
  4. Pair Device (app/device/pair.tsx)

    • Support ?method=code query param to pre-select "Enter Code" mode

Bug Fixes

  • Mobile stuck on "Connecting to server..." after tunnel restart (race condition)
  • Manual pairing code on Add Device page always failed (relay address was required)
  • add-device.tsx showed "Relay Address Required" dialog even in tunnel mode

Test Plan

  • Kill cloudflared → mobile auto-reconnects within ~10s
  • App foreground/background reconnect works
  • Manual pairing code on Add Device page works
  • QR scan pairing works
  • Sessions usable after reconnect

comlibmb and others added 3 commits May 4, 2026 13:12
- Add pollTunnelUrl (10s interval) to detect tunnel URL changes and auto-reconnect
- Add AppState foreground handler to check URL changes and reconnect on app resume
- Limit Socket.IO reconnection attempts to 3 to prevent conflicts with manual reconnect
- Fix add-device.tsx: redirect "Enter Code" to device/pair.tsx (proven pairing flow)
- Support ?method=code query param in pair.tsx to pre-select code entry mode
- Remove competing checkTunnelUrlOnDisconnect to eliminate reconnection race conditions

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When tunnel URL remains the same but connection drops, pollTunnelUrl
now triggers a reconnect instead of only handling URL changes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- pollTunnelUrl no longer does same-URL reconnect (prevents killing in-progress Socket.IO connections)
- Socket.IO handles short-term reconnects (10 attempts, 2-30s backoff)
- pollTunnelUrl only handles URL changes and fallback after Socket.IO gives up
- Added _socketIoGaveUp flag to coordinate between Socket.IO and pollTunnelUrl
- Foreground handler now retries 6 times with 5s intervals (30s total)
- Removed dead code: checkTunnelUrlOnDisconnect, _tunnelCheckInProgress
- Added RECONNECTION.md design document

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant